<?php
/**
 * 《站内消息》实现类
 *
 * @author 孙晓晔
 * @version $Id: Zone.class.php 804 2009-12-30 10:05:41Z sunxy $
 */
require_once(dirname(__FILE__) . '/LogUtil.class.php');

class Message extends LogUtil {
	public $tbl = Constant::tbl_emp_message;

	public $type = array(
		'-' => '请选择',
		'1' => '收件箱',
		'2' => '发件箱',
	);

	public $status = array(
		'-' => '请选择',
		'1' => '未读',
		'2' => '已读',
	
		'9' => '其它',
	);

	public $orderby = array(
		'-' => '请选择',
		'from' => '发送人',
		'to' => '接受人',

		'1' => '创建时间',
		'2' => '更新时间',
	);

	function __construct() {
		$request['update'] = '$Date: 2009/02/16 07:36:07 $';
		$request['revision'] = '$Revision: 1.1 $';

		parent::__construct($request);
	}


	function pretreat($controller) {
		parent::pretreat($controller);

		$controller->assign_by_ref('type_option', $this->type);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function find($controller, $request) {
		$this->pretreat($controller);

		$controller->display($request, 'log/message/find');
		return true;
	}

	function _orderby_x($controller, $row, $px) {
		$orderby = $this->orderby['1'];
		$row['orderby_x'] = $row['createtime'];

		if (empty($px) || $px == '-' || $px == '1') {
			$controller->assign('orderby', $orderby);

			return $row;
		}

		if ($px == '2') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['modifytime'];
		}

		$controller->assign('orderby', $orderby);

		return $row;
	}

	function _where($request) {
		$ip_s = $request['ip_s'];
		$where = " WHERE 1=1";

		if (!empty($ip_s)) {
			$where .= " AND ip like '" . $ip_s . "%'";
		}

		$where .= $this->_where_default($request);
		$where = $this->_where_aa($where);

		return $where;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function findPost($controller, $request) {
		$this->pretreat($controller);

		$this->findPara($controller, $request);

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);

		$pager_option = array (
            'db' => $db,
            'sql' => $sql,
            'PageSize' => $this->pagesize,
            'CurrentPageID' => $request['p'],
            'numItems' => $request['n']
		);

		$pager = @new Pager($pager_option);
		$data = $pager->getPageData();

		if ($pager->number > 0) {

			$count = 0;
			$from = $pager->from;
			while ($count < $pager->number) {
				$row = $data[$count];
				$row = $this->_view(null, $row);

				$row = $this->_orderby_x($controller, $row, $this->orderby_s);

				$data[$count] = $row;
				$count++;
			}
			$controller->assign_by_ref('result', $data);
			$controller->assign_by_ref('pager', $pager);
		}

		$controller->display($request, 'log/message/list');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function add($controller, $request) {
		$this->pretreat($controller);

		$controller->display($request, 'log/message/add');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function addPost($controller, $request) {
		$this->pretreat($controller);

		$from = $this->_input($request['from']);
		$to = $this->_input($request['to']);
		$subject = addslashes($request['subject']);
		$content = addslashes($request['content']);

		$db = newdb($this->dsn);

		$fmt = "INSERT INTO %s(`from`, `to`, subject, content, createtime, modifytime)";
		$fmt .= " VALUES('%s', '%s', '%s', '%s', now(), now())";
		$sql = sprintf($fmt, $this->tbl, $from, $to, $subject, $content);
		debug($sql);
		$db->query($sql);

		$request['id_s'] = $db->insert_id();
		
		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function view($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'info/message/view');
		return true;
	}
	
	function _view($id = 0, $row = null) {
		if ($row == null) {
			clean($this->tbl . "_id_" . $id);
			$row = $this->id($this->tbl, $id);
		}
		
		$row['from_t'] = Name::emp($row['from']);
		$row['to_t'] = Name::emp($row['to']);
		
		$row['sendmail_t'] = $this->sendmail[$row['sendmail']];
		
		$row = $this->_view_t($row);

		return $row;
	}

	function mail($controller, $request) {
		$db = newdb($this->dsn_w);
		$fmt = "SELECT * FROM %s WHERE sendmail = 1";
		$sql = sprintf($fmt, $this->tbl);
		$result = $db->query($sql);
		while ($row = $db->fetch_array($result)) {
			$from = $this->_email($row['from']);
			$to = $this->_email($row['to']);

			if (empty($from) || empty($to)) {
				echo "S";
				continue;
			}

			// $to = 'sunxy@tecmold.com';
			$this->sendmail($from, $to, $row['subject'], $row['content']);

			$fmt = "UPDATE %s SET sendmail = 2 WHERE id = '%s'";
			$sql = sprintf($fmt, $this->tbl, $row['id']);
			$db->query($sql);
		}

		logdate($controller, $request);
		return true;
	}

	function _email($emp) {
		$emp = $this->id(Constant::tbl_emp, $emp);
		if (empty($emp['email'])) {
			return '';
		}

		return $emp['email'];
	}

	function z($controller, $request) {
		$ip = '222.202.96.88';
		$mask = '255.255.255.128';

		$a = ip2long($mask) & ip2long($ip);

		echo long2ip($a);

		logdate($controller, $request);
		return true;
	}
}
?>